/*
 * ListenerJTable.java
 * Copyright (c) 2006, FMap
 *
 * Created on 27 de octubre de 2005, 1:04
 */

package fmap.gui.actionListener;

import fmap.DatosCompartidos;
import fmap.model.MapF;
import fmap.model.Node;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.table.TableModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author  Javier Sanroman Araujo
 * @version $Revision: 1.1
 */
public class ListenerJTablePersonal extends MouseAdapter implements ListSelectionListener, KeyListener
{
    /**
     * Creates a new instance of ListenerJTable
     */
    public ListenerJTablePersonal ()
    {
    }
    
    
    public void valueChanged (ListSelectionEvent e)
    {
        
        int rowSelect       = DatosCompartidos.getJTablePersonal ().getSelectedRow ();
        TableModel tm       = DatosCompartidos.getJTablePersonal ().getModel ();
        
        if( rowSelect != -1 )
        {
            Object instanciaDe = (Object)tm.getValueAt (rowSelect, 0);
            fmap.model.Node n = (fmap.model.Node)tm.getValueAt (rowSelect, 0);
            
            DatosCompartidos.setNodeActual ( n );
            
            // Mostramos en el panel derecho el card de los nodos
            java.awt.CardLayout layout = (java.awt.CardLayout)DatosCompartidos.getPanelRight ().getLayout ();
            layout.show (DatosCompartidos.getPanelRight (), "cardNode");
            // --------------------------------------------------------
            
            DatosCompartidos.getPanelRight ().getTextFName ().setText ( n.getName () );
            DatosCompartidos.getPanelRight ().getTextFDate ().setText ( n.getDateString ());
            DatosCompartidos.getPanelRight ().getTextFSize ().setText ( n.getSizeString () );
            DatosCompartidos.getPanelRight ().getTextPKey ().setText ( n.getKeyWord () );
            DatosCompartidos.getPanelRight ().getTextPComent ().setText ( n.getDescrip () );
            DatosCompartidos.getPanelRight ().setIcon ( n.getIcon () );
            
        }
        
    }
    
    
    public void mousePressed (MouseEvent e)
    {
        if(e.getClickCount () == 2) // Doble Click
        {
            irSubNodos ();
        }
    }//public void mousePressed
    
    
    // Implementamos los metodos de nuestro KeyListener
    // --------------------------------------------------------------------------------------------------------------------
    
    /** Handle the key typed event from the text field. */
    public void keyTyped (KeyEvent e)
    {
//        displayInfo (e, "KEY TYPED: ");
    }
    
    /** Handle the key pressed event from the text field. */
    public void keyPressed (KeyEvent e)
    {
//        displayInfo (e, "KEY PRESSED: ");
    }
    
    /** Handle the key released event from the text field. */
    public void keyReleased (KeyEvent e)
    {
        if (e.getKeyCode () == KeyEvent.VK_ENTER)
        {
            irSubNodos ();
        }
    }
    // --------------------------------------------------------------------------------------------------------------------
    
    
    /**
     * Si el nodo actual es un directorio, obtiene y envia al JList los subnodos del nodo actual
     */
    private void irSubNodos ()
    {
        int rowSelect       = DatosCompartidos.getJTablePersonal ().getSelectedRow ();
        
        if( rowSelect != -1 )
        {
            TableModel tm       = DatosCompartidos.getJTablePersonal ().getModel ();
            Node n              = (Node)tm.getValueAt (rowSelect, 0);
            
            
            // Solo si es un nodo de tipo directorio pasamos a los subNodos
            if ( n.getType () == 0 )
            {
                Vector v = new fmap.dao.NodeDao ().selectNodes ( n.getIdMapF (), n.getIdNode (), n.getIdTableSub () );
                n.setNode ( v );
                DatosCompartidos.getJTablePersonal ().setData ( n.getVNodeTable () );
                
                
                
                // Ahora hacemos que se expanda el nodo del jtree que corresponde con el nodo del jlist seleccionado
                TreePath nodoSeleccionado = DatosCompartidos.getJTreePersonal ().getSelectionPath ();
                
                if( nodoSeleccionado != null )
                {
                    Object nodo = (Object)nodoSeleccionado.getLastPathComponent ();
                    
                    if ( nodo instanceof Node )
                    {
                        Node nJTree = (Node)nodo;
                        
                        if ( nJTree.getVNode ().size ()==0 )    nJTree.setNode ( v );
                        
                        v = nJTree.getVNodeType (0);
                        
                    }
                    else if ( nodo instanceof MapF )
                    {
                        MapF nJTree = (MapF)nodo;
                        
                        if ( nJTree.getVNode ().size ()==0 )    nJTree.setVNode ( v );
                        
                        v = nJTree.getVNodeType (0);
                        
                    }
                    
                    // Añadimos al treepath actual del jtree el nodo seleccionado del jlist
                    nodoSeleccionado = nodoSeleccionado.pathByAddingChild (n);
                    DatosCompartidos.getJTreePersonal ().setSelectionPath ( nodoSeleccionado );
                    DatosCompartidos.getJTreePersonal ().expandPath ( nodoSeleccionado );
                    
                }
            } //if ( n.getType () == 0 )
        } //if( rowSelect != -1 )
    }
}